﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraControl : MonoBehaviour
{
    public Transform mainObj;
    Transform player1;
    Transform player2;

  
    public Vector3 initialDistance = new Vector3(0, 1.9f, 0);
    public bool followJumpingCharacter;
    public float smooth = 20;
    public float minZoom = 5;
    public float maxZoom = 54;
    public float maxDistance = 22;
    public float Dictance_YZ=8;
    public float leftBoundary = -38;
    public float rightBoundary = 38;
    public float minY = 0;
    public float maxY = 10;
    public bool EnableheightLimit;
    public float HeightLimit;
    public bool EnbaleYValue;
    public float YValue = 0.1f;
    public float ZValue = 1;//z轴变化率
    public float addsmooth = 20;
    public bool JumpY;
    public Vector3 JumpCameraZ;
    public float JumpZSmooth;
    public bool CinemaOver;
    public Vector3 engleBefore;
    public Vector3 posBefore;
    Quaternion rot = Quaternion.LookRotation(Vector3.zero);
    // Use this for initialization
    void Start()
    {
        if (VisualEntityManager.Instance.GetAllPlayerEntity().Count >= 2)
        {
            player1 = VisualEntityManager.Instance.GetAllPlayerEntity()[0].handle.transform;
            player2 = VisualEntityManager.Instance.GetAllPlayerEntity()[1].handle.transform;
        }
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void FixedUpdate()
    {
        if (VisualEntityManager.Instance.GetAllPlayerEntity().Count < 2)
        {
            return;
        }
        if(player1==null)
            player1 = VisualEntityManager.Instance.GetAllPlayerEntity()[0].handle.transform;
        if (player2==null)
            player2 = VisualEntityManager.Instance.GetAllPlayerEntity()[1].handle.transform;
    
    Vector3 newPosition = Vector3.zero;
        newPosition = ((player1.position + player2.position) / 2) + initialDistance;
        if (followJumpingCharacter)//摄像机跟随角色上下移动
            newPosition.y += Mathf.Abs(player1.position.y - player2.position.y) / 2;
        if (EnbaleYValue)//Y轴变化率
            newPosition.y = newPosition.y - (Vector3.Distance(player1.position, player2.position)) * YValue;
        CameraHeightLimit(ref newPosition);
        newPosition.x = Mathf.Clamp(newPosition.x, leftBoundary, rightBoundary);

        Limit_Dictance_YZ(ref newPosition);
        newPosition.y = Mathf.Clamp(newPosition.y, minY, maxY);
        newPosition.z = Mathf.Clamp(newPosition.z, minZoom, maxZoom);
        mainObj.GetComponent<Camera>().fieldOfView  = Mathf.Lerp(mainObj.GetComponent<Camera>().fieldOfView, 60, Time.fixedDeltaTime * smooth);
        mainObj.transform.position = Vector3.Lerp(mainObj.transform.position, newPosition, Time.fixedDeltaTime * smooth);
        PlayerJump();
        Camera_CinemaOver(newPosition);


    }

    void CameraHeightLimit(ref Vector3 newPosition)
    {
        if (EnableheightLimit)//摄像机最大高度设定
        {
            if (newPosition.y >= HeightLimit)
            {
                newPosition.y = HeightLimit;
                Vector3 _lookAtPosition = ((player1.position + player2.position) / 2f);
                Vector3 dir = _lookAtPosition - mainObj.transform.position;
                Quaternion rot = Quaternion.LookRotation(dir);
                mainObj.transform.rotation = Quaternion.Slerp(mainObj.transform.rotation, rot, Time.fixedDeltaTime * smooth);
            }
        }
    }

    void Limit_Dictance_YZ(ref Vector3 newPosition)
    {
        if (Vector3.Distance(player1.position, player2.position) > Dictance_YZ)//限制人物之间Yz的距离
        {
            newPosition.z = initialDistance.z - (Vector3.Distance(player1.position, player2.position)) * ZValue;
            //if (!EnbaleYValue)
            //    newPosition.y = newPosition.y - (Vector3.Distance(player1.position, player2.position)) * ZValue * Mathf.Tan((transform.localEulerAngles.x + mainObj.GetComponent<Camera>().fieldOfView / 2f) * Mathf.PI / 180f) * Mathf.Cos((transform.localEulerAngles.y - 180f) * Mathf.PI / 180f);
        }
        else
        {
            newPosition.z = initialDistance.z - (Dictance_YZ) * ZValue;
            //if (!EnbaleYValue)
            //    newPosition.y = newPosition.y - (Dictance_YZ) * ZValue * Mathf.Tan((transform.localEulerAngles.x + mainObj.GetComponent<Camera>().fieldOfView / 2f) * Mathf.PI / 180f) * Mathf.Cos((transform.localEulerAngles.y - 180f) * Mathf.PI / 180f);
        }
    }

    void PlayerJump()
    {
        if ((player1.position + player2.position).y >= 0 && JumpY)
        {
            float rate = (maxDistance - Mathf.Abs(player1.position.x - player2.position.x)) / maxDistance;
            Vector3 pos = mainObj.transform.position + (-JumpCameraZ) * rate * Mathf.Abs(player1.position.y - player2.position.y);
            pos.y = Mathf.Clamp(pos.y, minY, maxY);
            pos.z = Mathf.Clamp(pos.z, minZoom, maxZoom);
            mainObj.transform.position = Vector3.Lerp(mainObj.transform.position, pos, Time.fixedDeltaTime * JumpZSmooth);
        }
    }

    void Camera_CinemaOver(Vector3 newPosition)
    {
        if (CinemaOver)
        {
            if (engleBefore == Vector3.zero)
            {
                mainObj.transform.localEulerAngles = new Vector3(0, 0, 0);
                mainObj.transform.position = Vector3.Lerp(mainObj.transform.position, newPosition, Time.fixedDeltaTime * smooth * int.MaxValue);
            }
            else
            {
                mainObj.transform.localEulerAngles = engleBefore;
                mainObj.transform.position = posBefore;
            }
            CinemaOver = false;

        }
        else
        {
            mainObj.transform.localRotation = Quaternion.Slerp(mainObj.transform.localRotation, rot, Time.fixedDeltaTime * addsmooth);
            mainObj.transform.localEulerAngles = new Vector3(0, mainObj.transform.localEulerAngles.y, 0);
        }
    }

}
